const express = require('express')
const mysql = require('mysql2')
const takechoosevalue = express.Router()

takechoosevalue.get('/postvalue', (req, res) => {
    function finallyscore(choosefen, answers) {
        let arr = []
        choosetitel = []
        let obj = JSON.parse(choosefen)
        let result = 0
        for (let i in obj) {
            choosetitel.push(obj[i])
            result += obj[i]
            arr.push(result)
        }

        let answer = JSON.parse(answers).toString().split(',')

        let sum = 0
        let arr2 = []
        for (let i = 0; i < arr.length; i++) {
            if (i > 0) {
                for (let j = arr[i - 1]; j < arr[i]; j++) {
                    sum += Number(answer[j])

                }
                arr2.push(sum)
            } else {
                for (let j = 0; j < arr[i]; j++) {
                    sum += Number(answer[j])

                }
                arr2.push(sum)
            }
        }
        let fresult = []
        for (let i = 0; i < arr2.length; i++) {
            if (i === 0) {
                fresult.push(arr2[i].toFixed(2))
            } else {
                fresult.push((arr2[i] - arr2[i - 1]).toFixed(2))
            }
        }
        //原始分
        // console.log(fresult)
        // 每一组题目数
        // console.log(choosetitel)
        //转化分
        let tranlatefen = []
        for (let i in fresult) {
            tranlatefen.push((((fresult[i] - choosetitel[i]) / (choosetitel[i] * 4)) * 100).toFixed(2))

        }
        // console.log(tranlatefen)
        return {
            tranlatefen,
            fresult,

        }
    }

    function tizhi(obj) {
        let reutrunarr = []
        // console.log(obj.tranlatefen)
        if (obj.tranlatefen[0] >= 60 && obj.tranlatefen.slice(1).every(item => item < 30)) {
            reutrunarr.push('是')

        }
        else if(obj.tranlatefen[0] >= 60 && obj.tranlatefen.slice(1).every(item => item < 40)){
            reutrunarr.push('基本是')
        }else {
            reutrunarr.push('否')
        }
        let newarr = obj.tranlatefen.slice(1)
        for (let i in newarr){
            if (newarr[i]>=40){
                reutrunarr.push('是')
            }else if (newarr[i]>=30 && newarr[i] <40){
                reutrunarr.push('基本是')
            }else {
                reutrunarr.push('否')
            }
        }
        return reutrunarr
    }
    function result2(fresult){
        let numresult = []
        fresult.forEach(item =>{
            if (item === '是'){
                numresult.push(1)
            }else if(item === '基本是'){
                numresult.push(2)
            }else {
                numresult.push('')
            }
        })
        return numresult
    }
    const db = mysql.createPool({
        host: '127.0.0.1',
        user: 'root',
        password: '123',
        database: 'tianhetaidb2019'
    })
    let obj = finallyscore(req.query.choosefen, req.query.answer)

    const sqlStr = 'select * from tbconstitutiontype ';
    db.query(sqlStr, (err, result) => {
        if (err) console.log(err)
        let fresult = tizhi(obj)
        let numresult = result2(fresult)
        // console.log(numresult)
        // console.log(result)
        let tizhichoose = []
        result.forEach(item => tizhichoose.push(item.CtName))
        res.send({
            obj,
            fresult,
            tizhichoose,
            numresult
        })
    })


    // console.log(obj)

})

module.exports = takechoosevalue